//
//  ReadFunctionPage.m
//  KidReading
//
//  Created by telen on 15/11/3.
//  Copyright © 2015年 刘赞黄Telen. All rights reserved.
//

#import "ReadFunctionPage.h"
#import "ConstDefine.h"
#import "MDRadialProgressTheme.h"
#import "MDRadialProgressView.h"
#import "FuncCollectionCell.h"
#import "ReadBookVOrH_VC+Activity.h"

@interface ReadFunctionPage()<UICollectionViewDelegate,UICollectionViewDataSource>
@end
@implementation ReadFunctionPage
{
    MDRadialProgressView * radialView;
    UIImageView* _bookCoverImageView;
    UICollectionView* collectionView_guessYouLike;
    UICollectionView* collectionView_readFriends;
    UICollectionView* collectionView_tellStory;
    NSMutableArray* _bookInfoList;
    GridModel* _sameSeriesGridModel;
    NSArray* _friendsList;
    NSArray* _storiesList;
    //
    BOOL isScreenVertical;
    //
    CGFloat _ratio;
    //
    UIView* _tellStoriesView;
    UIView* _readFirendsView;
    UIView* _guessLikeView;
    UIView* _lineOneView;
    UIView* _lineTwoView;
    UIButton* _btn_sameSeries;
    UIButton* _btn_change;
    //
    NSDictionary* _sourceImageDict;
    NSArray* _spritImageArray;
    //
    TakePhotoViewController* _takePhotoVC;
    //
    UIButton* _btn_takePhoto;
    UIButton* _btn_quiz;
    UIButton* _btn_iReadIDo;
    CGRect frame_btnQuiz;
    CGRect frame_btnPhoto;
}

@synthesize bookCoverImageView = _bookCoverImageView;

+ (instancetype)readFuntionPageView_frame:(NSValue*)frameValue
{
    CGRect frame = frameValue.CGRectValue;
    ReadFunctionPage* view = [[self alloc] initWithFrame:frame];
    view.backgroundColor = [UIColor whiteColor];
    [view getBuddyListAndReloadView];
    [view getBorderAndSpritDataFromNet];
    return view;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        //
        if (frame.size.width > frame.size.height) {
            isScreenVertical = NO;
        }else{
            isScreenVertical = YES;
        }
        //
        CGFloat ratio = 1;
        ReadBookVOrH_VC* readVC = [ReadBookVOrH_VC currentReadInstance];
        //数据处理
        CGFloat w = frame.size.width;
        if (frame.size.width > frame.size.height) {
            ratio /= w/1024;
        }else{
            ratio /= w/768;
        }
        _ratio = ratio;
        //
        CGFloat func_bg_h = 314/ratio;
        CGFloat func_bg_w = w;
        //
        NSString* bgStr = @"read_func_bg.png";
        CGRect frame_cover = CGRectMake(w-(210+516)/ratio, 76/ratio, 225/ratio, 220/ratio);
        CGRect frame_endImageView = CGRectMake(w-(336+76)/ratio, 96/ratio, 336/ratio, 48/ratio);
        //CGRect frame_activityBtn = CGRectMake(w - (20 + 80)/ratio, 145/ratio, 88/ratio, 84/ratio);
        CGRect frame_btnReread = CGRectMake(w-(395+60)/ratio, 200/ratio, 70/ratio, 110/ratio);
        frame_btnQuiz = CGRectMake(w-(277+80)/ratio, 180/ratio, 90/ratio, 120/ratio);
        frame_btnPhoto =CGRectMake(w-(160+90)/ratio, 180/ratio, 90/ratio, 120/ratio);
        CGRect frame_btnFav = CGRectMake(w-(60+70)/ratio, 200/ratio, 70/ratio, 110/ratio);
        CGRect frame_btnTalk = CGRectMake(w-(27+67)/ratio, 93/ratio, 67/ratio, 59/ratio);
        //
        CGRect frame_viewReadFriends = CGRectMake(0, 310/ratio, w, 190/ratio);
        CGRect frame_titleIcon_readFriends = CGRectMake(20/ratio, 20/ratio, 22/ratio, 27/ratio);
        CGRect frame_titleLabel_readFriends = CGRectMake(54/ratio, 18/ratio, 300/ratio, 30/ratio);
        CGRect frame_collectionView_readFriends = CGRectMake(0, 60/ratio, w, 120/ratio);
        //
        CGRect frame_lineOneView = CGRectMake(0, 500 / ratio, w, 10 / ratio);
        //
        CGRect frame_viewTellStories = CGRectMake(0, 510/ratio, w, 190/ratio);
        CGRect frame_titleIcon_TellStories = CGRectMake(20/ratio, 20/ratio, 22/ratio, 27/ratio);
        CGRect frame_titleLabel_TellStories = CGRectMake(54/ratio, 18/ratio, 300/ratio, 30/ratio);
        CGRect frame_collectionView_TellStories = CGRectMake(0, 60 / ratio, w, 120/ratio);
        //
        CGRect frame_lineTwoView = CGRectMake(0, 700 / ratio, w, 10 / ratio);
        //
        CGRect frame_viewGuessLike = CGRectMake(0, 710/ratio, w, 324/ratio);
        CGRect frame_titleIcon_guessLike = CGRectMake(20/ratio, 20/ratio, 22/ratio, 27/ratio);
        CGRect frame_titleLabel_guessLike = CGRectMake(54/ratio, 18/ratio, 280/ratio, 30/ratio);
        CGRect frame_btnChange_guessLike = CGRectMake(w-(120+100)/ratio, 20/ratio, 100/ratio, 30/ratio);
        CGRect frame_btnSameSeries_guessLike = CGRectMake(w-(40+100)/ratio, 20/ratio, 100/ratio, 30/ratio);
        CGRect frame_colletionView_gueseLike = CGRectMake(0/ratio, 60/ratio, w, 270/ratio);
        if ([DataProcessing shareSingle].deviceType == DevicesTypes_iPhone) {
            frame_titleIcon_guessLike.origin.y -= 5;
            frame_titleLabel_guessLike.origin.y -= 5;
            frame_btnChange_guessLike.origin.y -= 5;
            frame_colletionView_gueseLike.origin.y -= 10;
        }

        if (self.linkQuizBookId == 0 && ![self.operMap[@"iReadIDo"][@"isShow"] boolValue]) {
            frame_btnPhoto.origin.x -= 50 / ratio;
        }
        if (!isScreenVertical) { //横着
            bgStr = @"";
            func_bg_h = frame.size.height;
            func_bg_w = 290 / ratio;
            //
            frame_cover = CGRectMake(w-(210+780)/ratio, 80/ratio, 225/ratio, 220/ratio);
            frame_endImageView = CGRectMake(w-(269+740)/ratio, 334/ratio, 269/ratio, 39/ratio);
            //CGRect frame_activityBtn = CGRectMake(w - (20 + 80)/ratio, 145/ratio, 88/ratio, 84/ratio);
            frame_btnReread = CGRectMake(w-(930+70)/ratio, 425/ratio, 70/ratio, 110/ratio);
            frame_btnQuiz = CGRectMake(w-(930+70)/ratio, 555/ratio, 70/ratio, 110/ratio);
            frame_btnPhoto =CGRectMake(w-(760+70)/ratio, 425/ratio, 70/ratio, 110/ratio);
            frame_btnFav = CGRectMake(w-(845+70)/ratio, 425/ratio, 70/ratio, 110/ratio);
            frame_btnTalk = CGRectMake(w-(845+70)/ratio, 555/ratio, 70/ratio, 110/ratio);
            //
            frame_viewReadFriends = CGRectMake(290 / ratio, 0/ratio, w - 290 / ratio, 260/ratio);
            frame_titleIcon_readFriends = CGRectMake(22/ratio, 75/ratio, 22/ratio, 27/ratio);
            frame_titleLabel_readFriends = CGRectMake(57/ratio, 75/ratio, 300/ratio, 30/ratio);
            frame_collectionView_readFriends = CGRectMake(0, 120/ratio, w - 290 / ratio, 120/ratio);
            //
            frame_lineOneView = CGRectMake(290 / ratio, 260 / ratio, w - 290 / ratio, 6 / ratio);
            //
            frame_viewTellStories = CGRectMake(290 / ratio, 267/ratio, w - 290 / ratio, 190/ratio);
            frame_titleIcon_TellStories = CGRectMake(20/ratio, 20/ratio, 22/ratio, 27/ratio);
            frame_titleLabel_TellStories = CGRectMake(54/ratio, 18/ratio, 300/ratio, 30/ratio);
            frame_collectionView_TellStories = CGRectMake(0, 60 / ratio, w - 290 / ratio, 120/ratio);
            //
            frame_lineTwoView = CGRectMake(290 / ratio, 456 / ratio, w - 290 / ratio, 6 / ratio);
            //
            frame_viewGuessLike = CGRectMake(290 / ratio, 463/ratio, w - 290 / ratio, 307/ratio);
            frame_titleIcon_guessLike = CGRectMake(20/ratio, 15/ratio, 22/ratio, 27/ratio);
            frame_titleLabel_guessLike = CGRectMake(54/ratio, 14/ratio, 280/ratio, 30/ratio);
            frame_btnChange_guessLike = CGRectMake(w-(120+100+290)/ratio, 20/ratio, 100/ratio, 30/ratio);
            frame_btnSameSeries_guessLike = CGRectMake(w-(40+100+290)/ratio, 20/ratio, 100/ratio, 30/ratio);
            frame_colletionView_gueseLike = CGRectMake(0/ratio, 70/ratio, w - 290 / ratio, 250/ratio);

            if (self.linkQuizBookId == 0 && ![self.operMap[@"iReadIDo"][@"isShow"] boolValue]) {
                frame_btnTalk.origin.x -= 85 / ratio;
            }
        }
        //UI元素
        UIImageView* imgvc = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, func_bg_w, func_bg_h)];
        imgvc.backgroundColor = [UIColor colorWithHexString:@"#F0F1F5"];
        imgvc.image = [UIImage imageNamed:bgStr];
        [self addSubview:imgvc];
        //
        _bookCoverImageView = [[UIImageView alloc] initWithFrame:frame_cover];
        _bookCoverImageView.contentMode = UIViewContentModeScaleAspectFit;
        [_bookCoverImageView JRSD_setImageWithURL:[NSURL URLWithString:readVC.coverImageUrl] placeholderImage:readVC.coverImg];
        [self addSubview:_bookCoverImageView];
        //
        UIImageView* endImageView = [[UIImageView alloc] initWithFrame:frame_endImageView];
        NSString* imageName = [DataProcessing chooseLanguageFromCN:@"read_func_txt_orange.png" andEN:@"read_func_txt_english.png"];
        endImageView.image = [UIImage imageNamed:imageName];
        endImageView.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:endImageView];
        //活动标志
        if (readVC.isCatched) {
//            UIButton* activityBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//            activityBtn.frame = frame_activityBtn;
//            [activityBtn addTouchScaleTransitions];
//            activityBtn.adjustsImageWhenHighlighted = NO;
//            [activityBtn sd_setBackgroundImageWithURL:[NSURL URLWithString:readVC.operMap[@"hwInfo"][@"decorate"]] forState:UIControlStateNormal completed:nil];
//            [activityBtn addTarget:self action:@selector(clickActivityFlag:) forControlEvents:UIControlEventTouchUpInside];
//            [self addSubview:activityBtn];
        };
        //重读
        UIButton* btn_Reread = [UIButton buttonWithType:UIButtonTypeCustom];
        btn_Reread.frame = frame_btnReread;
        [btn_Reread setImage:[UIImage imageNamed:@"read_func_btn_reread.png"] forState:UIControlStateNormal];
        [btn_Reread setImage:[UIImage imageNamed:@"read_func_btn_reread.png"] forState:UIControlStateHighlighted];
        [btn_Reread setTitle:NSLocalizedString(@"startOver", "") forState:UIControlStateNormal];
        [btn_Reread setTitleColor:[UIColor colorWithHexString:@"#6C7CC5"] forState:UIControlStateNormal];
        btn_Reread.titleLabel.font = [UIFont systemFontOfSize:16.0 / ratio];
        [btn_Reread setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 40 / ratio, 0)];
        [btn_Reread setTitleEdgeInsets:UIEdgeInsetsMake(65 / ratio, -frame_btnReread.size.width, 0, 0)];
        [self addSubview:btn_Reread];
        [btn_Reread addTouchScaleTransitions];
        [btn_Reread addTarget:self action:@selector(cliecked_Rereading:) forControlEvents:UIControlEventTouchUpInside];
        
        //收藏
        UIButton* btn_Fav = [UIButton buttonWithType:UIButtonTypeCustom];
        btn_Fav.frame = frame_btnFav;
        [btn_Fav setImage:[UIImage imageNamed:@"read_func_btn_collect_normal.png"] forState:UIControlStateNormal];
        [btn_Fav setImage:[UIImage imageNamed:@"read_func_btn_collect_pressed.png"] forState:UIControlStateHighlighted];
        [btn_Fav setImage:[UIImage imageNamed:@"read_func_btn_collect_pressed.png"] forState:UIControlStateSelected];
        [btn_Fav setTitle:NSLocalizedString(@"favorite", "") forState:UIControlStateNormal];
        [btn_Fav setTitleColor:[UIColor colorWithHexString:@"#6C7CC5"] forState:UIControlStateNormal];
        btn_Fav.titleLabel.font = [UIFont systemFontOfSize:16.0 / ratio];
        [btn_Fav setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 40 / ratio, 0)];
        [btn_Fav setTitleEdgeInsets:UIEdgeInsetsMake(65 / ratio, -frame_btnFav.size.width, 0, 0)];
        [self addSubview:btn_Fav];
        [btn_Fav addTouchScaleTransitions];
        [btn_Fav addTarget:self action:@selector(cliecked_Fav:) forControlEvents:UIControlEventTouchUpInside];
        btn_Fav.selected = readVC.isFavorited;
        [readVC msgTo_menthed:@"setFavBtn_2:" refs:@[btn_Fav] needReturnValue:NO];
        //活动标志
        if (self.linkQuizBookId == 0 && [self.operMap[@"iReadIDo"][@"isShow"] boolValue]) {
            UIButton* iReadIDoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            iReadIDoBtn.frame = frame_btnQuiz;
            if (isScreenVertical) {
                [iReadIDoBtn setImage:[UIImage imageNamed:@"read_func_btn_newstory.png"] forState:UIControlStateNormal];
                [iReadIDoBtn setImage:[UIImage imageNamed:@"read_func_btn_newstory.png"] forState:UIControlStateHighlighted];
            }else{
                [iReadIDoBtn setImage:[UIImage imageNamed:@"read_func_btn_newstory_H.png"] forState:UIControlStateNormal];
                [iReadIDoBtn setImage:[UIImage imageNamed:@"read_func_btn_newstory_H.png"] forState:UIControlStateHighlighted];
            }
            
            [iReadIDoBtn setTitle:NSLocalizedString(@"rewrite", "") forState:UIControlStateNormal];
            [iReadIDoBtn setTitleColor:[UIColor colorWithHexString:@"#6C7CC5"] forState:UIControlStateNormal];
            iReadIDoBtn.titleLabel.font = [UIFont systemFontOfSize:16.0 / ratio];
            if (!isScreenVertical) {
                [iReadIDoBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 40 / ratio, 0)];
                [iReadIDoBtn setTitleEdgeInsets:UIEdgeInsetsMake(65 / ratio, -frame_btnPhoto.size.width, 0, 0)];
            }else{
                [iReadIDoBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 30 / ratio, 0)];
                [iReadIDoBtn setTitleEdgeInsets:UIEdgeInsetsMake(95 / ratio, -frame_btnPhoto.size.width, 0, 0)];
            }
            [self addSubview:iReadIDoBtn];
            [iReadIDoBtn addTouchScaleTransitions];
            [iReadIDoBtn addTarget:self action:@selector(clickIReadIDoBtn:) forControlEvents:UIControlEventTouchUpInside];
        }
        //小测试
        if (self.linkQuizBookId != 0) {
            UIButton* btn_Quiz = [UIButton buttonWithType:UIButtonTypeCustom];
            btn_Quiz.frame = frame_btnQuiz;
            if (isScreenVertical) {
                [btn_Quiz setImage:[UIImage imageNamed:@"read_func_btn_test.png"] forState:UIControlStateNormal];
                [btn_Quiz setImage:[UIImage imageNamed:@"read_func_btn_test.png"] forState:UIControlStateHighlighted];
            }else{
                [btn_Quiz setImage:[UIImage imageNamed:@"read_func_btn_test_H.png"] forState:UIControlStateNormal];
                [btn_Quiz setImage:[UIImage imageNamed:@"read_func_btn_test_H.png"] forState:UIControlStateHighlighted];
            }
            [btn_Quiz setTitle:NSLocalizedString(@"quiz", "") forState:UIControlStateNormal];
            [btn_Quiz setTitleColor:[UIColor colorWithHexString:@"#6C7CC5"] forState:UIControlStateNormal];
            btn_Quiz.titleLabel.font = [UIFont systemFontOfSize:16.0 / ratio];
            [btn_Quiz setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 30 / ratio, 0)];
            [btn_Quiz setTitleEdgeInsets:UIEdgeInsetsMake(95 / ratio, -frame_btnQuiz.size.width, 0, 0)];
            if (!isScreenVertical) {
                [btn_Quiz setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 40 / ratio, 0)];
                [btn_Quiz setTitleEdgeInsets:UIEdgeInsetsMake(65 / ratio, -frame_btnQuiz.size.width, 0, 0)];
            }
            [self addSubview:btn_Quiz];
            [btn_Quiz addTouchScaleTransitions];
            [btn_Quiz addTarget:self action:@selector(cliecked_Quiz:) forControlEvents:UIControlEventTouchUpInside];
        }
        
        //拍照留念
        UIButton* btn_takePhoto = [UIButton buttonWithType:UIButtonTypeCustom];
        _btn_takePhoto = btn_takePhoto;
        btn_takePhoto.frame = frame_btnPhoto;
        if (isScreenVertical) {
            [btn_takePhoto setImage:[UIImage imageNamed:@"read_func_btn_photo.png"] forState:UIControlStateNormal];
            [btn_takePhoto setImage:[UIImage imageNamed:@"read_func_btn_photo.png"] forState:UIControlStateHighlighted];
        }else{
            [btn_takePhoto setImage:[UIImage imageNamed:@"read_func_btn_photo_H.png"] forState:UIControlStateNormal];
            [btn_takePhoto setImage:[UIImage imageNamed:@"read_func_btn_photo_H.png"] forState:UIControlStateHighlighted];
        }
        
        [btn_takePhoto setTitle:NSLocalizedString(@"photoFinish", "") forState:UIControlStateNormal];
        [btn_takePhoto setTitleColor:[UIColor colorWithHexString:@"#6C7CC5"] forState:UIControlStateNormal];
        btn_takePhoto.titleLabel.font = [UIFont systemFontOfSize:16.0 / ratio];
        if (!isScreenVertical) {
            [btn_takePhoto setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 40 / ratio, 0)];
            [btn_takePhoto setTitleEdgeInsets:UIEdgeInsetsMake(65 / ratio, -frame_btnPhoto.size.width, 0, 0)];
        }else{
            [btn_takePhoto setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 30 / ratio, 0)];
            [btn_takePhoto setTitleEdgeInsets:UIEdgeInsetsMake(95 / ratio, -frame_btnPhoto.size.width, 0, 0)];
        }
        [self addSubview:btn_takePhoto];
        [btn_takePhoto addTouchScaleTransitions];
        [btn_takePhoto addTarget:self action:@selector(cliecked_TakePhoto:) forControlEvents:UIControlEventTouchUpInside];
        
        //talk
        if (readVC.talkInfo != nil) {
            //
            UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = frame_btnTalk;
            if (isScreenVertical) {
                [btn setBackgroundImage:[UIImage imageNamed:@"read_func_btn_talk.png"] forState:UIControlStateNormal];
            }else{
                [btn setTitle:@"Talk" forState:UIControlStateNormal];
                [btn setTitleColor:[UIColor colorWithHexString:@"#6C7CC5"] forState:UIControlStateNormal];
                btn.titleLabel.font = [UIFont systemFontOfSize:16.0 / ratio];
                [btn setImage:[UIImage imageNamed:@"read_func_btn_talk_H.png"] forState:UIControlStateNormal];
                [btn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 40 / ratio, 0)];
                [btn setTitleEdgeInsets:UIEdgeInsetsMake(65 / ratio, -frame_btnTalk.size.width, 0, 0)];
            }
            [self addSubview:btn];
            [btn addTouchScaleTransitions];
            _talk_btn = btn;
            _isClicedTalkBtn = NO;
            [_talk_btn addTarget:self action:@selector(clicked_TalkBtn) forControlEvents:UIControlEventTouchUpInside];
        }
        //读过书的小伙伴
        UIView* readFriendsView = [[UIView alloc] initWithFrame:frame_viewReadFriends];
        readFriendsView.backgroundColor = [UIColor whiteColor];
        [self addSubview:readFriendsView];
        _readFirendsView = readFriendsView;
        //Title
        UIImageView* titleImageView_readFriends = [[UIImageView alloc] initWithFrame:frame_titleIcon_readFriends];
        titleImageView_readFriends.image = [UIImage imageNamed:@"read_func_icon_read.png"];
        [readFriendsView addSubview:titleImageView_readFriends];
        UILabel* titleLabel_readFriends = [[UILabel alloc] initWithFrame:frame_titleLabel_readFriends];
        titleLabel_readFriends.text = ReadLocalizedString(@"otherReadUser");
        titleLabel_readFriends.font = [UIFont systemFontOfSize:22 / ratio];
        titleLabel_readFriends.textColor = [UIColor colorWithHexString:@"#616161"];
        [readFriendsView addSubview:titleLabel_readFriends];
        //UICollectionView
        CGFloat ratio_w = [DataProcessing shareSingle].ratio_w;
        CGFloat item_h_readF = 80/ratio_w;
        UICollectionViewFlowLayout *layout_readF = [[UICollectionViewFlowLayout alloc] init];
        layout_readF.minimumInteritemSpacing = 25 / ratio;
        layout_readF.itemSize = CGSizeMake(item_h_readF,item_h_readF * 4 / 3);
        layout_readF.sectionInset = UIEdgeInsetsMake(0, 40 / ratio, 0, 40 / ratio);
        if (!isScreenVertical) {
            layout_readF.minimumInteritemSpacing = 50 / ratio;
            layout_readF.itemSize = CGSizeMake(item_h_readF,item_h_readF);
            layout_readF.sectionInset = UIEdgeInsetsMake(10, 40 / ratio, 0, 40 / ratio);
            if ([DataProcessing shareSingle].deviceType == DevicesTypes_iPhone) {
                layout_readF.sectionInset = UIEdgeInsetsMake(0, 40 / ratio, 0, 40 / ratio);
            }
        }
        collectionView_readFriends = [[UICollectionView alloc] initWithFrame:frame_collectionView_readFriends collectionViewLayout:layout_readF];
        collectionView_readFriends.clipsToBounds = NO;
        collectionView_readFriends.backgroundColor = [UIColor clearColor];
        [readFriendsView addSubview:collectionView_readFriends];
        collectionView_readFriends.scrollEnabled = NO;
        collectionView_readFriends.delegate = self;
        collectionView_readFriends.dataSource = self;
        [collectionView_readFriends registerClass:[UpTopCollectionViewCell class] forCellWithReuseIdentifier:_id_UpTopCollectionViewCell];
        [collectionView_readFriends registerNib:[UINib nibWithNibName:@"UpTopCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:_id_UpTopCollectionViewCell];
        //
        UIView* lineOneView = [[UIView alloc] initWithFrame:frame_lineOneView];
        lineOneView.backgroundColor = [UIColor colorWithHexString:@"#F0F1F5"];
        [self addSubview:lineOneView];
        _lineOneView = lineOneView;
        //小伙伴讲得故事
        UIView* tellStoriesView = [[UIView alloc] initWithFrame:frame_viewTellStories];
        tellStoriesView.backgroundColor = [UIColor whiteColor];
        [self addSubview:tellStoriesView];
        _tellStoriesView = tellStoriesView;
        //Title
        UIImageView* titleImageView_tellStories = [[UIImageView alloc] initWithFrame:frame_titleIcon_TellStories];
        titleImageView_tellStories.image = [UIImage imageNamed:@"read_func_icon_read.png"];
        [tellStoriesView addSubview:titleImageView_tellStories];
        UILabel* titleLabel_tellStories = [[UILabel alloc] initWithFrame:frame_titleLabel_TellStories];
        titleLabel_tellStories.text = ReadLocalizedString(@"userStories");
        titleLabel_tellStories.font = [UIFont systemFontOfSize:22 / ratio];
        titleLabel_tellStories.textColor = [UIColor colorWithHexString:@"#616161"];
        [tellStoriesView addSubview:titleLabel_tellStories];
        //
        CGFloat item_h_tellStory = 90/ratio_w;
        UICollectionViewFlowLayout *layout_tellStory = [[UICollectionViewFlowLayout alloc] init];
        layout_tellStory.minimumInteritemSpacing = 25 / ratio;
        layout_tellStory.itemSize = CGSizeMake(item_h_tellStory,item_h_tellStory);
        layout_tellStory.sectionInset = UIEdgeInsetsMake(15 / ratio, 40 / ratio, 0, 40 / ratio);
        if (!isScreenVertical) {
            tellStoriesView.backgroundColor = [UIColor clearColor];
            layout_tellStory.sectionInset = UIEdgeInsetsMake(15 / ratio, 40 / ratio, 0, 40 / ratio);
        }
        collectionView_tellStory = [[UICollectionView alloc] initWithFrame:frame_collectionView_TellStories collectionViewLayout:layout_tellStory];
        collectionView_tellStory.backgroundColor = [UIColor clearColor];
        [tellStoriesView addSubview:collectionView_tellStory];
        collectionView_tellStory.scrollEnabled = NO;
        collectionView_tellStory.delegate = self;
        collectionView_tellStory.dataSource = self;
        collectionView_tellStory.clipsToBounds = YES;
        [collectionView_tellStory registerClass:[FuncBottleCollectionViewCell class] forCellWithReuseIdentifier:_id_FuncBottleCollectionViewCell];
        [collectionView_tellStory registerNib:[UINib nibWithNibName:@"FuncBottleCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:_id_FuncBottleCollectionViewCell];
        //
        UIView* lineTwoView = [[UIView alloc] initWithFrame:frame_lineTwoView];
        lineTwoView.backgroundColor = [UIColor colorWithHexString:@"#F0F1F5"];
        [self addSubview:lineTwoView];
        _lineTwoView = lineTwoView;
        //
        //猜你喜欢
        UIView* guessLikeView = [[UIView alloc] initWithFrame:frame_viewGuessLike];
        guessLikeView.backgroundColor = [UIColor whiteColor];
        [self addSubview:guessLikeView];
        _guessLikeView = guessLikeView;
        //
        imgvc = [[UIImageView alloc] initWithFrame:frame_titleIcon_guessLike];
        imgvc.image = [UIImage imageNamed:@"read_func_icon_read.png"];
        [guessLikeView addSubview:imgvc];
        UILabel* label = [[UILabel alloc] initWithFrame:frame_titleLabel_guessLike];
        label.text = ReadLocalizedString(@"guessUlike");
        label.font = [UIFont systemFontOfSize:22/ratio];
        label.textColor = [UIColor colorWithHexString:@"#616161"];
        [guessLikeView addSubview:label];
        
        //同系列
        UIButton* btn_sameSeries = [UIButton buttonWithType:UIButtonTypeCustom];
        btn_sameSeries.frame = frame_btnSameSeries_guessLike;
        btn_sameSeries.titleLabel.font = [UIFont systemFontOfSize:22/ratio];
        //btn_sameSeries.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [btn_sameSeries setTitle:ReadLocalizedString(@"sameSeriesbtn") forState:UIControlStateNormal];
        [btn_sameSeries setTitleColor:[UIColor colorWithHexString:@"#7E8FDA"] forState:UIControlStateNormal];
        [guessLikeView addSubview:btn_sameSeries];
        [btn_sameSeries addTouchScaleTransitions];
        [btn_sameSeries addTarget:self action:@selector(goSameSeries:) forControlEvents:UIControlEventTouchUpInside];
        [btn_sameSeries JRMAS_makeConstraints:^(JRMASConstraintMaker *make) {
            make.right.JRMAS_equalTo(-30 / ratio);
            make.centerY.JRMAS_equalTo(imgvc);
        }];
        _btn_sameSeries = btn_sameSeries;
        //换一换
        UIButton* btn_change = [UIButton buttonWithType:UIButtonTypeCustom];
        btn_change.frame = frame_btnChange_guessLike;
        btn_change.titleLabel.font = [UIFont systemFontOfSize:22/ratio];
        //btn_change.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [btn_change setTitle:ReadLocalizedString(@"changebtn") forState:UIControlStateNormal];
        [btn_change setTitleColor:[UIColor colorWithHexString:@"#F89838"] forState:UIControlStateNormal];
        [guessLikeView addSubview:btn_change];
        [btn_change addTouchScaleTransitions];
        [btn_change addTarget:self action:@selector(refreshCollectionView:) forControlEvents:UIControlEventTouchUpInside];
        [btn_change JRMAS_makeConstraints:^(JRMASConstraintMaker *make) {
            make.right.JRMAS_equalTo(btn_sameSeries.JRMAS_left).offset(-20 / ratio);
            make.centerY.JRMAS_equalTo(imgvc);
        }];
        _btn_change = btn_change;
        //CollectionView
        CGFloat item_h_guessLike = 240/ratio_w;
        if ([DataProcessing shareSingle].deviceType == DevicesTypes_iPhone){
            if (frame.size.height > frame.size.width) {
                item_h_guessLike += 20/ratio;
            }
        }
        UICollectionViewFlowLayout *layout_guessLike = [[UICollectionViewFlowLayout alloc] init];
        layout_guessLike.minimumInteritemSpacing = -20 / ratio;
        layout_guessLike.itemSize = CGSizeMake(item_h_guessLike*10.0/13.0,item_h_guessLike);
        layout_guessLike.sectionInset = UIEdgeInsetsMake(0, 20 / ratio, 0, 20 / ratio);
        if ([DataProcessing shareSingle].deviceType == DevicesTypes_iPhone) {
            layout_guessLike.minimumInteritemSpacing = 0;
            layout_guessLike.sectionInset = UIEdgeInsetsMake(0, 40 / ratio, 0, 40 / ratio);
        }
        if (!isScreenVertical) {
            layout_guessLike.sectionInset = UIEdgeInsetsMake(-10 / ratio, 25 / ratio, 0, 20 / ratio);
            if ([DataProcessing shareSingle].deviceType == DevicesTypes_iPhone){
                layout_guessLike.sectionInset = UIEdgeInsetsMake(0, 25 / ratio, 0, 20 / ratio);
                layout_guessLike.minimumInteritemSpacing = -20 / ratio;
            }
        }
        collectionView_guessYouLike = [[UICollectionView alloc] initWithFrame:frame_colletionView_gueseLike collectionViewLayout:layout_guessLike];
        collectionView_guessYouLike.backgroundColor = [UIColor clearColor];
        [guessLikeView addSubview:collectionView_guessYouLike];
        collectionView_guessYouLike.scrollEnabled = NO;
        collectionView_guessYouLike.delegate = self;
        collectionView_guessYouLike.dataSource = self;
        collectionView_guessYouLike.clipsToBounds = YES;
        [collectionView_guessYouLike registerClass:[FuncCollectionCell class] forCellWithReuseIdentifier:_id_FuncCollectionCell];
        [collectionView_guessYouLike registerNib:[UINib nibWithNibName:@"FuncCollectionCell" bundle:nil] forCellWithReuseIdentifier:_id_FuncCollectionCell];
        //
        [DataProcessing do_Async_quick:^{
            [self refreshCollectionView:nil];
        }];
    }
    return self;
}
//获取拍照边框以及本书是否存在精灵
-(void)getBorderAndSpritDataFromNet{
    [[NetGetPostsWithCache shareInstance] getPhotoBorderStyleAndSpritImageWithBookId:[ReadBookVOrH_VC currentReadInstance].bookid Success:^(NSDictionary *retDict, BOOL hasCache) {
        if (retDict) {
            [self dealSourceImageDataWithRetDict:retDict];
        }
    } failure:^(NSError *error, BOOL hasCache) {
        
    } cache:^(NSDictionary *cacheRetDict) {
        if (cacheRetDict) {
            [self dealSourceImageDataWithRetDict:cacheRetDict];
        }
    }];
}
//处理数据
-(void)dealSourceImageDataWithRetDict:(NSDictionary*)retDict{
    if (retDict) {
        _sourceImageDict = retDict;
        //
        NSArray* spritUrlArray = nil;
        if ([retDict[@"data"] isKindOfClass:[NSDictionary class]]) {
            spritUrlArray = retDict[@"data"][@"photographs"];
        }
        
        if (spritUrlArray.count > 0) {
            [self downLoadHomeAniImageWithDict:spritUrlArray AllComplete:^(NSArray *imageArray) {
                _spritImageArray = imageArray;
                if (_takePhotoVC) {
                    _takePhotoVC.spritImageArray = imageArray;
                    [_takePhotoVC reflashSpritImageAni];
                }
            }];
        }
    }
}
//
//
-(void)downLoadHomeAniImageWithDict:(NSArray*)activityUrlArray AllComplete:(void(^)(NSArray* imageArray))allComplete{
    [DownLoadActivityImage downloadActivityImageWithImageUrlArray:activityUrlArray ActivityName:__FantasyTown Type:@([ReadBookVOrH_VC currentReadInstance].bookid).description AllComplete:^(NSArray *imageArray) {
        if (allComplete) {
            allComplete(imageArray);
        }
    }];
}
//
- (void)exchangeBookImage:(UIImage*)bookImage{
    if (bookImage) {
        _bookCoverImageView.image = bookImage;
    }
}
//请求读过书的小伙伴和讲过故事的小伙伴
-(void)getBuddyListAndReloadView{
    [[NetGetPostsWithCache shareInstance] getFuncPageBuddyListWithBookId:[ReadBookVOrH_VC currentReadInstance].bookid Success:^(NSDictionary *retDict, BOOL hasCache) {
        [self handleFuncDataWithRetDict:retDict];
    } failure:^(NSError *error, BOOL hasCache) {
        NSLog(@"%@",error);
    } cache:^(NSDictionary *cacheRetDict) {
        if (cacheRetDict) {
            [self handleFuncDataWithRetDict:cacheRetDict];
        }
    }];
}
//处理数据并刷新视图
-(void)handleFuncDataWithRetDict:(NSDictionary*)retDict{
    [DataProcessing do_Main:^{
        NSString* avatarPath = retDict[@"bookPhoto"];
        if (avatarPath && ![avatarPath isEqualToString:@""]) {
            [_bookCoverImageView JRSD_setImageWithURL:[NSURL URLWithString:avatarPath]];
        }
        self.linkQuizBookId = [retDict[@"linkedQuizBookId"] integerValue];
        self.operMap = retDict[@"operMap"];
        //刷新按钮显示
        [self reflashBtnShow];
        //
        NSArray* tellStoryList = retDict[@"audioList"];
        NSArray* readFriendsList = retDict[@"readList"];
        if (tellStoryList.count == 0) {
            _tellStoriesView.hidden = YES;
            //
            if (readFriendsList.count == 0) {
                _readFirendsView.hidden = YES;
                _lineTwoView.hidden = YES;
                if (isScreenVertical) {
                    _guessLikeView.frame = CGRectMake(0, 310/_ratio, _guessLikeView.width, 324/_ratio);
                }else{
                    _guessLikeView.frame = CGRectMake(290 / _ratio, 70/_ratio, _guessLikeView.width, 307/_ratio);
                }
            }else{
                _readFirendsView.hidden = NO;
                _lineTwoView.hidden = NO;
                if (isScreenVertical) {
                    _guessLikeView.frame = CGRectMake(0, 510/_ratio, _guessLikeView.width, 324/_ratio);
                }else{
                    _guessLikeView.frame = CGRectMake(290 / _ratio, 267/_ratio, _guessLikeView.width, 307/_ratio);
                }
            }
        }else{
            _tellStoriesView.hidden = NO;
            //
            if (readFriendsList.count == 0) {
                _readFirendsView.hidden = YES;
                _lineTwoView.hidden = NO;
                if (isScreenVertical) {
                    _tellStoriesView.frame = CGRectMake(0, 310/_ratio, _tellStoriesView.width, 190/_ratio);
                    _guessLikeView.frame = CGRectMake(0, 510/_ratio, _guessLikeView.width, 324/_ratio);
                }else{
                    _tellStoriesView.frame = CGRectMake(290 / _ratio, 75/_ratio, _tellStoriesView.width, 190/_ratio);
                    _guessLikeView.frame = CGRectMake(290 / _ratio, 267/_ratio, _guessLikeView.width, 307/_ratio);
                }
            }else{
                _readFirendsView.hidden = NO;
                _lineTwoView.hidden = NO;
                if (isScreenVertical) {
                    _guessLikeView.frame = CGRectMake(0, 710/_ratio, _guessLikeView.width, 324/_ratio);
                }else{
                    _guessLikeView.frame = CGRectMake(290 / _ratio, 463/_ratio, _guessLikeView.width, 307/_ratio);
                }
            }
        }
        
        if (tellStoryList) {
            _storiesList = tellStoryList;
            [collectionView_tellStory reloadData];
        }
        if (readFriendsList) {
            _friendsList = readFriendsList;
            [collectionView_readFriends reloadData];
        }
        
    } delay:0.0];
}

-(void)reflashBtnShow{
    if (self.linkQuizBookId != 0) {
        if (_btn_iReadIDo) {
            [self removeBtn_iReadIDoCompleted:^{
                [self addBtn_quizCompleted:nil];
            }];
        }
        else if (!_btn_quiz) {
            [self addBtn_quizCompleted:nil];
        }
    }else{
        if ([self.operMap[@"iReadIDo"][@"isShow"] boolValue]) {
            if (_btn_quiz) {
                [self removeBtn_QuizCompleted:^{
                    [self addBtn_iReadIDoCompleted:nil];
                }];
            }
            else if (!_btn_iReadIDo) {
                [self addBtn_iReadIDoCompleted:nil];
            }
        }else{
            [self removeBtn_iReadIDoCompleted:nil];
        }
    }
}
//
-(void)addBtn_quizCompleted:(void(^)())comp{
    if (_btn_quiz) {
        return;
    }
    UIButton* btn_Quiz = [UIButton buttonWithType:UIButtonTypeCustom];
    _btn_quiz = btn_Quiz;
    btn_Quiz.frame = frame_btnQuiz;
    if (isScreenVertical) {
        [btn_Quiz setImage:[UIImage imageNamed:@"read_func_btn_test.png"] forState:UIControlStateNormal];
        [btn_Quiz setImage:[UIImage imageNamed:@"read_func_btn_test.png"] forState:UIControlStateHighlighted];
    }else{
        [btn_Quiz setImage:[UIImage imageNamed:@"read_func_btn_test_H.png"] forState:UIControlStateNormal];
        [btn_Quiz setImage:[UIImage imageNamed:@"read_func_btn_test_H.png"] forState:UIControlStateHighlighted];
    }
    [btn_Quiz setTitle:NSLocalizedString(@"quiz", "") forState:UIControlStateNormal];
    [btn_Quiz setTitleColor:[UIColor colorWithHexString:@"#6C7CC5"] forState:UIControlStateNormal];
    btn_Quiz.titleLabel.font = [UIFont systemFontOfSize:16.0 / _ratio];
    [btn_Quiz setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 30 / _ratio, 0)];
    [btn_Quiz setTitleEdgeInsets:UIEdgeInsetsMake(95 / _ratio, -frame_btnQuiz.size.width, 0, 0)];
    if (!isScreenVertical) {
        [btn_Quiz setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 40 / _ratio, 0)];
        [btn_Quiz setTitleEdgeInsets:UIEdgeInsetsMake(65 / _ratio, -frame_btnQuiz.size.width, 0, 0)];
    }
    [self addSubview:btn_Quiz];
    [btn_Quiz addTouchScaleTransitions];
    [btn_Quiz addTarget:self action:@selector(cliecked_Quiz:) forControlEvents:UIControlEventTouchUpInside];
    btn_Quiz.alpha = 0;
    //
    [UIView animateWithDuration:0.3f animations:^{
        if (isScreenVertical) {
            CGRect frame_takePhoto = _btn_takePhoto.frame;
            frame_takePhoto.origin.x += 50 / _ratio;
            _btn_takePhoto.frame = frame_takePhoto;
        }else{
            CGRect frame_talk = self.talk_btn.frame;
            frame_talk.origin.x += 85 / _ratio;
            self.talk_btn.frame = frame_talk;
        }
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.3 animations:^{
            btn_Quiz.alpha = 1;
        } completion:^(BOOL finished) {
            if (comp) {
                comp();
            }
        }];
    }];
}
-(void)addBtn_iReadIDoCompleted:(void(^)())comp{
    if (_btn_iReadIDo) {
        return;
    }
    UIButton* iReadIDoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _btn_iReadIDo = iReadIDoBtn;
    iReadIDoBtn.frame = frame_btnQuiz;
    if (isScreenVertical) {
        [iReadIDoBtn setImage:[UIImage imageNamed:@"read_func_btn_newstory.png"] forState:UIControlStateNormal];
        [iReadIDoBtn setImage:[UIImage imageNamed:@"read_func_btn_newstory.png"] forState:UIControlStateHighlighted];
    }else{
        [iReadIDoBtn setImage:[UIImage imageNamed:@"read_func_btn_newstory_H.png"] forState:UIControlStateNormal];
        [iReadIDoBtn setImage:[UIImage imageNamed:@"read_func_btn_newstory_H.png"] forState:UIControlStateHighlighted];
    }
    
    [iReadIDoBtn setTitle:NSLocalizedString(@"rewrite", "") forState:UIControlStateNormal];
    [iReadIDoBtn setTitleColor:[UIColor colorWithHexString:@"#6C7CC5"] forState:UIControlStateNormal];
    iReadIDoBtn.titleLabel.font = [UIFont systemFontOfSize:16.0 / _ratio];
    if (!isScreenVertical) {
        [iReadIDoBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 40 / _ratio, 0)];
        [iReadIDoBtn setTitleEdgeInsets:UIEdgeInsetsMake(65 / _ratio, -frame_btnPhoto.size.width, 0, 0)];
    }else{
        [iReadIDoBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 30 / _ratio, 0)];
        [iReadIDoBtn setTitleEdgeInsets:UIEdgeInsetsMake(95 / _ratio, -frame_btnPhoto.size.width, 0, 0)];
    }
    [self addSubview:iReadIDoBtn];
    [iReadIDoBtn addTouchScaleTransitions];
    [iReadIDoBtn addTarget:self action:@selector(clickIReadIDoBtn:) forControlEvents:UIControlEventTouchUpInside];
    iReadIDoBtn.alpha = 0;
    
    [UIView animateWithDuration:0.3f animations:^{
        if (isScreenVertical) {
            CGRect frame_takePhoto = _btn_takePhoto.frame;
            frame_takePhoto.origin.x += 50 / _ratio;
            _btn_takePhoto.frame = frame_takePhoto;
        }else{
            CGRect frame_talk = self.talk_btn.frame;
            frame_talk.origin.x += 85 / _ratio;
            self.talk_btn.frame = frame_talk;
        }
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.3 animations:^{
            iReadIDoBtn.alpha = 1;
        } completion:^(BOOL finished) {
            if (comp) {
                comp();
            }
        }];
    }];
}
//
-(void)removeBtn_QuizCompleted:(void(^)())comp{
    if (!_btn_quiz) {
        return;
    }
    [UIView animateWithDuration:0.3f animations:^{
        _btn_quiz.alpha = 0;
    } completion:^(BOOL finished) {
        [_btn_quiz removeFromSuperview],_btn_quiz = nil;
        [UIView animateWithDuration:0.3f animations:^{
            if (isScreenVertical) {
                CGRect frame_takePhoto = _btn_takePhoto.frame;
                frame_takePhoto.origin.x -= 50 / _ratio;
                _btn_takePhoto.frame = frame_takePhoto;
            }else{
                CGRect frame_talk = self.talk_btn.frame;
                frame_talk.origin.x -= 85 / _ratio;
                self.talk_btn.frame = frame_talk;
            }
        } completion:^(BOOL finished) {
            if (comp) {
                comp();
            }
        }];
    }];
}
//
-(void)removeBtn_iReadIDoCompleted:(void(^)())comp{
    if (!_btn_iReadIDo) {
        return;
    }
    [UIView animateWithDuration:0.3f animations:^{
        _btn_iReadIDo.alpha = 0;
    } completion:^(BOOL finished) {
        [_btn_iReadIDo removeFromSuperview],_btn_iReadIDo = nil;
        [UIView animateWithDuration:0.3f animations:^{
            if (isScreenVertical) {
                CGRect frame_takePhoto = _btn_takePhoto.frame;
                frame_takePhoto.origin.x -= 50 / _ratio;
                _btn_takePhoto.frame = frame_takePhoto;
            }else{
                CGRect frame_talk = self.talk_btn.frame;
                frame_talk.origin.x -= 85 / _ratio;
                self.talk_btn.frame = frame_talk;
            }
        } completion:^(BOOL finished) {
            if (comp) {
                comp();
            }
        }];
    }];
}

//
- (void)cliecked_Rereading:(UIButton*)btn
{
    [[ReadBookVOrH_VC currentReadInstance] msgTo_menthed:@"gotToCover" refs:@[] needReturnValue:NO];
    
    [NSObject msgToClassName:@"TLMobStat" menthed:@"logEvent:eventLabel:" refs:@[RE_main,@"功能页_重读"] needReturnValue:NO];
}

- (void)cliecked_Quiz:(UIButton*)btn{
    //
    [[ReadBookVOrH_VC currentReadInstance] tapBack];
    [DataProcessing do_Main:^{
        [[RootViewController shareLastInstance] openSingelBookWithBookId:_linkQuizBookId tbBook:nil isClick:YES];
    } delay:0.6];
}

-(void)cliecked_TakePhoto:(UIButton*)btn{
    //判断是否有授权并执行对应操作
    [self isAuthorizationForVideo];
}

- (void)cliecked_Fav:(UIButton*)btn
{
    [[ReadBookVOrH_VC currentReadInstance] msgTo_menthed:@"clickedFavBtn" refs:@[] needReturnValue:NO];
}

- (void)clicked_TalkBtn
{
    _isClicedTalkBtn = YES;
}
//判断是否授权
-(void)isAuthorizationForVideo{
    AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    switch (status) {
        case AVAuthorizationStatusAuthorized:
            [self presentTakePhotoVC];
            break;
            
        case AVAuthorizationStatusRestricted:
        case AVAuthorizationStatusDenied:
            [UIAlertView tl_showTitle:nil message:NSLocalizedString(@"accessCamera", "") didClick:^(UIAlertView * _Nullable alertView, NSInteger buttonIndex) {
                if (buttonIndex == 1) {
                    NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
                    if([[UIApplication sharedApplication] canOpenURL:url]) {
                        NSURL*url =[NSURL URLWithString:UIApplicationOpenSettingsURLString];
                        [[UIApplication sharedApplication] openURL:url];
                    }
                }
            } buttonTitles:NSLocalizedString(@"cancel", ""),NSLocalizedString(@"settings", ""),nil];
            break;
            
        case AVAuthorizationStatusNotDetermined:
            [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
                if (granted) {
                    [self presentTakePhotoVC];
                }
            }];
            break;
    }
}
//
-(void)presentTakePhotoVC{
    ReadBookVOrH_VC* readVC = [ReadBookVOrH_VC currentReadInstance];
    TakePhotoViewController* takePhotoVC = [[TakePhotoViewController alloc] init];
    _takePhotoVC = takePhotoVC;
    takePhotoVC.sourceUrlsDict = _sourceImageDict;
    takePhotoVC.spritImageArray = _spritImageArray;
    takePhotoVC.funcPage = self;
    takePhotoVC.bookCoverImage = readVC.coverImg;
    takePhotoVC.bookid = readVC.bookid;
    takePhotoVC.hurdleNo = readVC.hurdleNo;
    takePhotoVC.activityId = readVC.activityId;
    takePhotoVC.bookName = readVC.bookName;
    TakePhotoNavigationViewController* takePhotoNVC = [[TakePhotoNavigationViewController alloc] initWithRootViewController:takePhotoVC];
    [DataProcess do_Main:^{
        [[ReadBookVOrH_VC currentReadInstance] presentViewController:takePhotoNVC animated:YES completion:nil];
    } delay:0.3];
}
//我读我创造
-(void)clickIReadIDoBtn:(UIButton*)button{
    ReadBookVOrH_VC* readVC = [ReadBookVOrH_VC currentReadInstance];
    [readVC tapBack];
    [DataProcess do_Main:^{
        WebViewController *webVC = [[WebViewController alloc] init];
        NSString* url = self.operMap[@"iReadIDo"][@"target"];
        if (![url hasPrefix:@"http"]) {
            url = [DataProcessing full_BaseUrl:__Ser3_0_URL_Base_ url:url];
        }
        if (url.length>0) {
            webVC.url = url;
            webVC.showType = SettingShowType_H5;
            webVC.isfullScrean = YES;
            [[RootViewController shareLastInstance].navigationController pushViewController:webVC animated:YES];
        }
    } delay:0.3];
}

//捉鬼活动
-(void)clickActivityFlag:(UIButton*)button{
    ReadBookVOrH_VC* readVC = [ReadBookVOrH_VC currentReadInstance];
    NSString* showText = [NSString stringWithFormat:@"%@\n\n%@",self.operMap[@"hwInfo"][@"gClue"],self.operMap[@"hwInfo"][@"funBottomInfo"]];
    [UIAlertView tl_showTitle:nil message:showText didClick:^(UIAlertView * _Nullable alertView, NSInteger buttonIndex) {
        if (buttonIndex == 0) {
            //去书库
            [readVC tapBack];
            [[RootViewController shareLastInstance].navigationController popToRootViewControllerAnimated:NO];
            [[RootViewController shareLastInstance] changeToToolBarType:@"booklib"];
        }else if (buttonIndex == 1){
            //我的捉鬼记录
            [readVC tapBack];
            [DataProcess do_Main:^{
                WebViewController *webVC = [[WebViewController alloc] init];
                NSString *url = [DataProcessing full_BaseUrl:__Ser3_0_URL_Base_ url:self.operMap[@"hwInfo"][@"ghostHistory"]];
                if (url.length>0) {
                    webVC.url = url;
                    webVC.showType = SettingShowType_H5;
                    webVC.isfullScrean = YES;
                    [[RootViewController shareLastInstance].navigationController pushViewController:webVC animated:YES];
                }
            } delay:0.3];
        }
    } buttonTitles:NSLocalizedString(@"goHome", nil),NSLocalizedString(@"myCatchGostList", nil),nil];
}

//
- (void)setDownTalkProgress:(NSInteger)numOf100
{
    CGFloat w = _talk_btn.bounds.size.width;
    if (!radialView) {
        //
        MDRadialProgressTheme *newTheme = [[MDRadialProgressTheme alloc] init];
        newTheme.completedColor = [UIColor colorWithRed:86/255.0 green:103/255.0 blue:198/255.0 alpha:1.0];
        newTheme.incompletedColor = [UIColor colorWithRed:126/255.0 green:143/255.0 blue:218/255.0 alpha:0.2];
        newTheme.centerColor = [UIColor colorWithWhite:1.0f alpha:0.35f];
        newTheme.sliceDividerHidden = YES;
        newTheme.labelColor = [UIColor colorWithRed:86/255.0 green:103/255.0 blue:198/255.0 alpha:1.0];
        newTheme.labelShadowColor = [UIColor whiteColor];
        newTheme.font = [UIFont boldSystemFontOfSize:w*0.12];
        newTheme.thickness = ceill(w*0.03/[DataProcessing shareSingle].ratio_w)*6;
        //
        CGRect frame = CGRectMake(0, 0, ceil((w)*0.75 / 2) * 2, ceil((w)*0.75 / 2) * 2);
        radialView =  [[MDRadialProgressView alloc] initWithFrame:frame andTheme:newTheme];
        radialView.center = _talk_btn.center;
        radialView.endString = @"%";
        [self addSubview:radialView];
        radialView.progressTotal = 100;
    }
    radialView.progressCounter = numOf100;
    _talk_btn.hidden = YES;
}

- (void)setDoneTalkProgress
{
    if (radialView && radialView.superview) {
        [radialView removeFromSuperview];
    }
    radialView = nil;
    _talk_btn.hidden = NO;
}


#pragma mark-
//
-(void)goSameSeries:(UIButton*)button{
//    [[ReadBookVOrH_VC currentReadInstance] tapBack];
//    //
//    MoreBooksViewController* mbvc = [[MoreBooksViewController alloc] init];
//    mbvc.type = MoreBooks_Type_More;
//    mbvc.showType = MoreShowType_More;
//    mbvc.orderByType = OrderByType_Net;
//    mbvc.gridModel = _sameSeriesGridModel;
//    mbvc.topTitle = [DataProcessing chooseLanguageFromCN:_sameSeriesGridModel.itemNameZh andEN:_sameSeriesGridModel.itemNameEn];
//    mbvc.collType = _sameSeriesGridModel.itemType;
//    [[RootViewController shareLastInstance].navigationController pushViewController:mbvc animated:YES];
}
//
- (void)refreshCollectionView:(UIButton*)sender
{
    void (^readyBooks)(NSDictionary*) = ^(NSDictionary* retDict){
        NSString* bookIdsStr = retDict[@"bookIds"];
        if (!bookIdsStr) {
            bookIdsStr = @"";
        }
        NSDictionary* allBookSubDict = retDict[@"bookSub"];
        NSDictionary* sameSeriesDict = retDict[@"similarSeries"];
        if (!sameSeriesDict.count) {
            [DataProcess do_Main_quick:^{
                [_btn_sameSeries JRMAS_makeConstraints:^(JRMASConstraintMaker *make) {
                    make.width.JRMAS_equalTo(0);
                }];
            }];
        }else{
            [DataProcess do_Main_quick:^{
                [_btn_sameSeries JRMAS_remakeConstraints:^(JRMASConstraintMaker *make) {
                    make.right.JRMAS_equalTo(-30 / _ratio);
                    make.centerY.JRMAS_equalTo(_btn_change);
                }];
            }];
        }
        _sameSeriesGridModel = [GridModel JRMJ_objectWithKeyValues:sameSeriesDict];
        //
        //LongLong currentTime = [[[NSString alloc] initWithFormat:@"%0.f",[[NSDate date] timeIntervalSince1970]*1000] longLongValue];
        [[DBHelper shareInstance] selectBookWithReadCountList_Books:bookIdsStr complete:^(NSDictionary *reDict) {
            NSMutableArray *bookMarr = [NSMutableArray new];
            NSMutableArray *bookModelArray = reDict[@"book"];
            [[DBHelper shareInstance] tbbooks_mutableArray:bookModelArray orderBy_BookIds:bookIdsStr];
            for (TABLE_BOOKS* bookModel in bookModelArray) {
                NSString* bookKey = [NSString stringWithFormat:@"%ld",(long)bookModel.bookId];
                NSDictionary *bookSubDict = allBookSubDict[bookKey];
                //
                [RootViewController mergeBookSubWithBaseBookTable:bookModel BookSub:bookSubDict];
                [bookMarr addObject:bookModel];
            }
            _bookInfoList = bookMarr;
            [DataProcessing do_Main_quick:^{
                [collectionView_guessYouLike reloadData];
            }];
        }];
    };
    
    if ([ReadBookVOrH_VC currentReadInstance]) {
        NSInteger bookid = [ReadBookVOrH_VC currentReadInstance].bookid;
        [[NetGetPostsWithCache shareInstance] readSimilarBookListForBookid:bookid Success:^(NSDictionary *retDict, BOOL hasCache) {
            readyBooks(retDict);
        } failure:^(NSError *error, BOOL hasCache) {
            
        } cache:^(NSDictionary *cacheRetDict) {
            if (_bookInfoList == nil || _bookInfoList.count == 0) {
                readyBooks(cacheRetDict);
            }
        }];
    }
    
    if (sender) {
        [NSObject msgToClassName:@"TLMobStat" menthed:@"logEvent:eventLabel:" refs:@[RE_main,@"功能页_换一换"] needReturnValue:NO];
    }
}

#pragma mark-
#pragma mark- CollectionViewDelegate,DataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSInteger count = 0;
    if (collectionView == collectionView_guessYouLike) {
        count = _bookInfoList.count;
        if (count > 4) {
            count = 4;
        }
        if ([DataProcessing shareSingle].deviceType == DevicesTypes_iPhone && self.frame.size.height > self.frame.size.width) {
            if (count > 3) {
                count = 3;
            }
        }
    }else if (collectionView == collectionView_readFriends){
        count = _friendsList.count;
        if (count > 6) {
            count = 6;
        }
        if (!isScreenVertical) {
            if (count > 5) {
                count = 5;
            }
        }
    }else if (collectionView == collectionView_tellStory){
        count = _storiesList.count;
        if (count > 6) {
            count = 6;
        }
        if (!isScreenVertical) {
            if (count > 5) {
                count = 5;
            }
        }
    }
    
    return count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell* cell = nil;
    CGFloat ratio = [DataProcessing shareSingle].ratio_w;
    if (collectionView == collectionView_guessYouLike) {
        TABLE_BOOKS* tb_book = _bookInfoList[indexPath.row];
        //
        FuncCollectionCell* bookCell = [collectionView dequeueReusableCellWithReuseIdentifier:_id_FuncCollectionCell forIndexPath:indexPath];
        cell = bookCell;
        //
        bookCell.imagview_downDoneIcon.hidden = YES;
        //
        bookCell.tb_book = tb_book;
        if ([DataProcessing shareSingle].deviceType == DevicesTypes_iPhone) {
            bookCell.lable_title.verticalAlignment = VerticalAlignmentMiddle;
            bookCell.lable_title.numberOfLines = 1;
            bookCell.lable_title.font = [UIFont systemFontOfSize:20/ratio];
        }else{
            bookCell.lable_title.font = [UIFont systemFontOfSize:12/ratio];
        }
        bookCell.lable_title.text = [DataProcessing chooseLanguageFromCN:tb_book.name_zh andEN:tb_book.name_en];
        NSString* url = [DataProcessing fullBookCoverImageUrl:tb_book.thumb];
        [bookCell.imageview_cover JRSD_setImageWithURL:[NSURL URLWithString:url] placeholderImage:nil options:JRSDWebImageRefreshCached];
        //
        //销售状态
        bookCell.salefag = tb_book.freeflag;
        NSString* file = nil;
        switch (bookCell.salefag) {
            case bookshelf_saleFlag_type_free:
                file = [DataProcessing languageForfileName:@"bookcase_icon_free.png"];
                break;
            case bookshelf_saleFlag_type_opus:
                file = @"bookcase_icon_cup.png";
                break;
            case bookshelf_saleFlag_type_LimitTime:
                file = [DataProcessing languageForfileName:@"bookcase_icon_limitedtime.png"];
                break;
            default:
                break;
        }
        UIImage* flagimg = nil;
        UserInfo* uinfo = [CacheData shareSingle].getUserLoginInfo;
        if ([uinfo isBoughtBookId:tb_book.bookId]) {
            file = [DataProcessing languageForfileName:@"bookcase_icon_paid.png"];
        }
        if(file)flagimg = [UIImage imageNamed:file];
        bookCell.imageview_flag.image = flagimg;
        //
        //处理书本状态
        if([[CacheData shareSingle] isBookDownDoneWithBookId:tb_book.bookId])
        {
            [bookCell setBookDownDone];
        }
        //newflag
        bookCell.newflag_imgv.hidden = !tb_book.newflag;
        
#if __Debug_BookID_Grid
        NSString* zipDate = @"Null";
        NSArray* partsArr = tb_book.partsArr;
        if (partsArr && partsArr.count >0) {
            id partItem = partsArr.firstObject;
            if ([partItem isKindOfClass:[NSDictionary class]]) {
                NSString* file = [partItem valueForKey:@"uri"];
                file = [file stringByDeletingLastPathComponent];
                zipDate = [file lastPathComponent];
            }else {
                NSString* file = partItem;
                file = [file stringByDeletingLastPathComponent];
                zipDate = [file lastPathComponent];
            }
        }
        bookCell.lable_title.text = [NSString stringWithFormat:@"[%ld:%ld][%ld][%ld]{%@} %@",(long)bookCell.tb_book.bookId,(long)bookCell.tb_book.originalId,(long)tb_book.itemId,(long)bookCell.tb_book.rCount,zipDate,bookCell.lable_title.text];
#endif
        //
        //事件
        __weak ReadFunctionPage* wself = self;
        bookCell.indexPath = indexPath;
        bookCell.didTouchCell = ^(FuncCollectionCell* cell){
            [wself didTouchCell:cell];
        };
    }else if (collectionView == collectionView_readFriends){
        NSDictionary* currentPerson = _friendsList[indexPath.item];
        UpTopCollectionViewCell* personCell = [collectionView dequeueReusableCellWithReuseIdentifier:_id_UpTopCollectionViewCell forIndexPath:indexPath];
        personCell.tapCellBlock = ^(UpTopCollectionViewCell* cell){
            [self clickPersonAvatarWithUid:[currentPerson[@"user_id"] integerValue]];
        };
        [DataProcessing SDW_setImageContainDefaultAvatarWithURL:[NSURL URLWithString:currentPerson[@"avatar"]] View:personCell.iconImageView Gender:[currentPerson[@"gender"] integerValue]];
        personCell.clipsToBounds = NO;
        personCell.iconImageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
        personCell.iconImageView.layer.cornerRadius = 40 / ratio;
        personCell.iconImageView.layer.borderWidth = 1;
        personCell.nameLabel.text = currentPerson[@"nickname"];
        personCell.userId = [currentPerson[@"user_id"] integerValue];
        cell = personCell;
    }else if (collectionView == collectionView_tellStory){
        NSDictionary* currentAudio = _storiesList[indexPath.item];
        FuncBottleCollectionViewCell* storyCell = [collectionView dequeueReusableCellWithReuseIdentifier:_id_FuncBottleCollectionViewCell forIndexPath:indexPath];
        storyCell.audioDict = currentAudio;
        [storyCell.bottleImageView JRSD_setImageWithURL:currentAudio[@"ugc_icon"]];
        storyCell.bookCoverImageView.image = [ReadBookVOrH_VC currentReadInstance].coverImg ? [ReadBookVOrH_VC currentReadInstance].coverImg : self.bookCoverImageView.image;
        storyCell.tapBottleBlock = ^(NSDictionary* audioDict){
            [self clickAudioBottleWithUrl:currentAudio[@"audio_url"]];
        };
        cell = storyCell;
    }
    //
    return cell;
}
//点击头像
-(void)clickPersonAvatarWithUid:(NSInteger)uid{
    if (uid) {
        ReadBookVOrH_VC* readVC = [ReadBookVOrH_VC currentReadInstance];
        //[readVC tapBack];
        //[[RootViewController shareLastInstance].navigationController popToRootViewControllerAnimated:NO];
        //[AboutMeViewController showUserinfoVCWithCurrentVC:[RootViewController shareLastInstance] targetId:uid];
       // [AboutMeViewController showUserinfoVCWithCurrentVC:readVC targetId:uid isPresent:YES];
    }
}
//点击魔瓶
-(void)clickAudioBottleWithUrl:(NSString*)url{
    if (url.length) {
        ReadBookVOrH_VC* readVC = [ReadBookVOrH_VC currentReadInstance];
        [DataProcess do_Main:^{
            WebViewController *webVC = [[WebViewController alloc] init];
            if (url.length>0) {
                webVC.url = url;
                webVC.showType = SettingShowType_H5;
                webVC.popUpType = WebPopUpType_Present;
                webVC.isfullScrean = YES;
                [readVC presentViewController:webVC animated:YES completion:nil];
            }
        } delay:0.3];
    }
}

- (void)didTouchCell:(FuncCollectionCell*)cell
{
    cell.tb_book.infoDefine = cell.imageview_cover.image;
    [[ReadBookVOrH_VC currentReadInstance] tapBack];
    [DataProcessing do_Main:^{
        [[RootViewController shareLastInstance] openSingelBookWithBookId:cell.tb_book.bookId tbBook:cell.tb_book isClick:YES];
    } delay:0.6];
}
//
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath];
    cell.transform = CGAffineTransformMakeScale(1.05f, 1.05f);
}

- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    [UIView animateWithDuration:0.1 delay:0.1 options:0 animations:^{
        UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath];
        cell.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
    } completion:nil];
}
@end
